package selef.xiafan.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import selef.xiafan.entity.system.SysRole;
import selef.xiafan.entity.user.User;
import selef.xiafan.query.SysRoleQ;
import selef.xiafan.service.ISysRolePermissionS;
import selef.xiafan.service.ISysRoleService;
import selef.xiafan.util.result.PageResult;
import selef.xiafan.util.result.Result;
import selef.xiafan.util.result.ResultFactory;

import java.util.Set;

/**
 * @author xiafan
 * @version 1.0
 * @date 2021/7/10 16:13
 */
@RestController
@RequestMapping("/api/role")
public class SysRoleController {
    @Autowired
    private ISysRoleService iSysRoleService;
    @Autowired
    private ISysRolePermissionS iSysRolePermissionS;
//    @Autowired
//    private ISysRoleMenuS iSysRoleMenuS;

    @PostMapping("/loginRole")
    public Set<String> loginRole(@RequestBody User user) {
        return iSysRoleService.getloginRoles(user);
    }

    @PostMapping("/selectedUserRoles")
    public Result selectedUserRoles(@RequestBody User user) {
        return ResultFactory.buildSuccessResult(iSysRoleService.selectedUserRoles(user));
    }

    @PostMapping("/listRole")
    public PageResult<SysRole> listRole(@RequestBody SysRoleQ sysRoleQ) {
        return iSysRoleService.getAll(sysRoleQ);
    }

    @GetMapping("/menusRole")
    public Result listRole() {
        return ResultFactory.buildSuccessResult(iSysRoleService.getAll());
    }

    @PostMapping("/addRole")
    public Result addRole(@RequestBody SysRole sysRole) {
        iSysRoleService.addorupdate(sysRole);
        return ResultFactory.buildSuccessResult("权限添加成功");
    }

    @PutMapping("/changeStatue")
    public Result changeStatue(@RequestBody SysRole requestSysRole) {
        SysRole sysRole = iSysRoleService.updateRoleStatue(requestSysRole);
        String message = "权限" + sysRole.getNameZh() + "状态更新成功";
        return ResultFactory.buildSuccessResult(message);
    }

    @PutMapping("/roleandperms/update")
    public Result updataRoleAndIntermediate(@RequestBody SysRole sysRole) {
        iSysRoleService.addorupdate(sysRole);
        return ResultFactory.buildSuccessResult("修改成功");
    }

//    @PutMapping("/update")
//    public Result updateRole(@RequestBody SysRole sysRole) {
//        iSysRoleS.addorupdate(sysRole);
//        iSysRolePermissionS.saveChange(sysRole.getId(), sysRole.getPerms());
//        return ResultFactory.buildSuccessResult("修改成功");
//    }

//    @PutMapping("/roleMenu/update")
//    public Result updateRoleMenu(@RequestBody Map<String, List<Integer>> menuid, @RequestParam Integer rid) {
//        iSysRoleMenuS.updateRoleMenu(menuid, rid);
//        return ResultFactory.buildSuccessResult("修改成功");
//    }

    @PostMapping("/deleteBy")
    public Result deleteByRole(@RequestBody SysRole role) {
        int result = iSysRoleService.deleteRole(role);
        if (result == 0) {
            return ResultFactory.buildFailResult("删除失败，请先解除启用状态");
        }
        return ResultFactory.buildSuccessResult("删除成功");
    }
}
